<template>
	<view class="index">
		<view class="list">
			<view 
				class="item u-flex u-row-between" 
				v-for="item,index of menus" 
				:key="index"
				@click="toDetail(item)"
			>
				<view class="image">
					<u-image 
						:src="item.main_image_cdn" 
						:lazy-load="true"
						width="240"
						mode="widthFix"
					></u-image>
				</view>
				<view class="info u-flex-col u-row-between">
					<view class="title">
						<text>{{item.title}}</text>
					</view>
					<view class="user u-flex u-row-between">
						<view class="left">
							<u-icon name="account"></u-icon>
							<text>{{item.user_text}}</text>
						</view>
						<view class="right">
							<text>{{item.view}}人看过</text>
						</view>
					</view>
					<view class="footer">
						<text class="score">{{item.score}}分</text>
					</view>
				</view>
			</view>
			<u-empty :show="menus.length == 0"></u-empty>
			<u-loadmore v-show="menus.length > 0" :status="loadStatus"></u-loadmore>
		</view>
	</view>
</template>

<script>
	import {mapState} from 'vuex'
	export default {
		data(){
			return {
				order: '',
				menus: [],
				page: 1,
				loadStatus: 'loadmore',
				categoryId: ''
			}
		},
		methods: {
			loadMenus(){
				this.loadStatus = 'loading'
				return this.$api.menu.index({
					category: this.categoryId,
					order: this.order,
					page: this.page
				})
				.then(res=>{
					if(res.code == 1 && res.data.last_page >= this.page){
						this.menus = this.menus.concat(res.data.data)
						this.loadStatus = 'loadmore'
					}else{
						this.loadStatus = 'nomore'
					}
				})
			},
			reload(categoryId){
				this.page = 1
				this.loadStatus = 'loadmore'
				this.menus = []
				this.order = ''
				if(categoryId !== null)
					this.categoryId = categoryId
				return this.loadMenus()
			},
			nextPage(){
				if(this.loadStatus == 'nomore') return
				this.page = this.page + 1
				return this.loadMenus()
			},
			toDetail(item){
				this.$Router.push({
					name: 'menu.detail',
					params: {
						id: item.id
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.index{
		.list{
			padding: $uni-spacing-col-base $uni-spacing-row-base;
			background-color: $uni-bg-color;
			.item{
				padding: $uni-spacing-col-base 0;
				.image{
					width: 240upx;
				}
				.info{
					width: 419upx;
					height: 195upx;
					.title{
						font-size: 30upx;
						font-weight: bold;
					}
					.user{
						color: $uni-text-color-grey;
						.left{
							text{
								margin-left: $uni-spacing-row-sm;
							}
						}
					}
					.footer{
						.score{
							font-size: 30upx;
							font-weight: bold;
							color: $uni-color-primary;
						}
					}
				}
			}
		}
	}
</style>
